package com.zzwtec.validation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.zzwtec.validation.annotation.DateFormat;

/**
 * 日期格式注解解析器
 * 
 * @author 草原狼
 *
 */
public class DateFormatValidator implements AnnotationValidator{
	
	/**
	 * 校验f字段的值是否符合value的日期格式
	 * @see DateFormat
	 */
	@Override
	public ValidateResult validate(Field f, Object value) {
		ValidateResult result = new ValidateResult();
		if(f.isAnnotationPresent(DateFormat.class)){
			DateFormat dateFormat = f.getAnnotation(DateFormat.class);
			try {
				if(value != null){
					SimpleDateFormat format = new SimpleDateFormat(dateFormat.format());
					format.parse(value.toString());
				}
			} catch (ParseException e) {
				result.setMessage(dateFormat.fieldName() + "不满足格式：" + dateFormat.format());
			}	
		}
		return result;
	}

	private String name;
	
	@Override
	public String getName() {		
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;		
	}

	@Override
	public Class<? extends Annotation> getAnnotationClass() {		 
		return DateFormat.class;
	}
	
}
